package dao.impl;

import dao.QuestionDao;
import entity.ExamType;
import entity.Question;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import utils.DbUtil;

import java.util.List;

/**
 * Created by honghao on 2017/2/9.
 */
public class QuestionDaoImpl implements QuestionDao {
    private static SessionFactory sf = DbUtil.getSf();

    @Override
    public Question get(int id) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        Question qs = (Question) session.get(Question.class, id);
        tx.commit();
        return qs;
    }

    @Override
    public void save(Question question) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.save(question);
        tx.commit();
    }

    @Override
    public void delete(Question question) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.delete(question);
        tx.commit();
    }

    @Override
    public void delete(int id) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        Question qu = (Question) session.get(Question.class, id);
        session.delete(qu);
        tx.commit();
    }

    @Override
    public void update(Question question) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.update(question);
        tx.commit();
    }

    @Override
    public List<Question> findAllByPage(int pageNo, int pageSize)
    {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        Query query = session.createQuery("from Question ");
        query.setFirstResult(pageNo);
        query.setMaxResults(pageSize);
        return query.list();
    }

    @Override
    public Question findQuestionByExamType(int id, ExamType examType) {
        return null;
    }

    @Override
    public int getMaxId() {
        return 0;
    }

    @Override
    public long getQuestionCount() {
        return 0;
    }
}
